﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Model;
using LinkCard.Dao;
using LinkCard.AdminCP.Common;
using System.Data;
using jobseekerLLBL;
using System.Data.SqlClient;
namespace LinkCard.AdminCP.Job
{
    public partial class job_new : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            //else
            //{
            //    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
            //    {
            //        if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
            //        {
            //            Response.Redirect("/message_box.aspx?why=1&msg=5");
            //        }
            //    }
            //}

            if (!IsPostBack)
            {
                DataTable dt;

                dt = DataHelper.SelectTable("SELECT CompanyID, CompanyName FROM Company ORDER BY CompanyName", false);
                CompanyID.DataSource = dt;
                CompanyID.DataTextField = "CompanyName";
                CompanyID.DataValueField = "CompanyID";
                CompanyID.DataBind();
                
                dt = DataHelper.SelectTable("SELECT CityID, CityName FROM Cities ORDER BY CityName", false);
                JobCities.DataSource = dt;
                JobCities.DataTextField = "CityName";
                JobCities.DataValueField = "CityID";
                JobCities.DataBind();

                dt = DataHelper.SelectTable("SELECT IndustryID, IndustryName FROM Industrial ORDER BY IndustryName", false);
                JobIndustries.DataSource = dt;
                JobIndustries.DataTextField = "IndustryName";
                JobIndustries.DataValueField = "IndustryID";
                JobIndustries.DataBind();

                dt = DataHelper.SelectTable("SELECT JobLevelID, JobLevelName FROM JobLevel ORDER BY JobLevelName", false);
                JobLevel.DataSource = dt;
                JobLevel.DataTextField = "JobLevelName";
                JobLevel.DataValueField = "JobLevelID";
                JobLevel.DataBind();
                JobPostDate.Value = DateTime.Today.ToString("dd/MM/yyyy");
                
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            add();
            Response.Redirect("/Job/job_all_published.aspx");
        }

        protected void add()
        {
            try
            {
                var item = new clsJobs();
                item.CompanyID = Convert.ToInt32(CompanyID.SelectedValue);
                item.JobPosition = JobPosition.Value;
                item.JobDescription = JobDescription.Value;
                item.JobRequire = JobRequire.Value;
                item.JobLevelID = Convert.ToInt32(JobLevel.SelectedValue);
                item.JobSalaryLevel = JobSalaryLevel.Value;
                item.JobType = JobType.Value;
                item.JobPostDate = Convert.ToDateTime(Utility.SwitchDateTime(JobPostDate.Value));
                if (JobHot.Checked) item.JobHot = 1;
                else item.JobHot = 0;
                item.JobDeadlineToApply = Convert.ToDateTime(Utility.SwitchDateTime(JobDeadlineToApply.Value));
                item.Insert();
                var cityItem = aJobCities.Value.Split(',');
                for (int i=0; i< cityItem.Count();i++)
                {
                    var test = Convert.ToInt32(cityItem[i]);
                    DataHelper.ExecuteNonQuery("cms_Job_Cities_Insert", new SqlParameter("@CityID", Convert.ToInt32(cityItem[i])));
                }
                var industryItem = aJobIndustries.Value.Split(',');
                for (int i = 0; i < industryItem.Count(); i++)
                {
                    DataHelper.ExecuteNonQuery("cms_Job_Industrial_Insert", new SqlParameter("@IndustryID", Convert.ToInt32(industryItem[i])));
                }
            }
            catch(Exception ex) { 
                
            }
        }

        protected Int64 getUserID()
        {

            DataTable dt = DataHelper.SelectTable("SELECT UserID FROM [User] WHERE Username ='" + Session["USER"].ToString() + "'", false);
            if (dt.Rows.Count > 0)
            {
                return ConvertUtility.ToInt64(dt.Rows[0]["UserID"]);
            }
            else
            {
                return 0;
            }
        }
    }
}
